<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Login extends MY_Controller {

    function login() {
        parent :: __construct();
        $this->load->model('settings_model');
        $this->template->set_theme('admin');
        $this->template->set_layout('admin_login');
    }
//-----------Admin login -----------------
    public function index() {
        $data['site_info'] = $this->settings_model->get_setting('webtitle');
        $this->session->unset_userdata('sess_admin_user');
        $this->template->build('pages/admin_login');
    }
//-----------Admin  Logout----------------
    function logout() {
        $this->session->unset_userdata('sess_admin_user');
        $this->session->unset_userdata('sess_admin_password');
        $this->session->unset_userdata('sess_admin_name');
        $this->session->unset_userdata('sess_admin_last_login');
        $this->session->unset_userdata('sess_admin_last_ip');
        $this->session->unset_userdata('sess_admin_status');
        redirect('admin/dashboard');
    }
//-----------Admin  Login Submit----------------
    public function submit() {
        $this->form_validation->set_rules('admin_user', 'admin_user', 'required|xss_clean');
        $this->form_validation->set_rules('admin_password', 'admin_password', 'required|xss_clean');
        if ($this->form_validation->run()) {
            $iuser = trim($this->input->post('admin_user',TRUE)); 
            $ipwd  = trim($this->input->post('admin_password',TRUE));
            
            $sql = "Select * from admin_user where status=51 and user='$iuser' and password='$ipwd'";
            $query = $this->db->query($sql);
            
            if ($query->num_rows() == 1) {
                $row = $query->row();
                $data = array
                    (
                    'last_login' => unix_to_human(time(), TRUE),
                    'last_ip' => $this->input->ip_address()
                );
                //--Update last_login n IP
                $this->db->where('user', $this->input->post('admin_user', TRUE));
                $this->db->update('admin_user', $data);

                // Our user exists, set session.
                $this->session->set_userdata('sess_admin_user', $row->user);
                $this->session->set_userdata('sess_admin_password', $row->password);
                $this->session->set_userdata('sess_admin_name', $row->name);
                $this->session->set_userdata('sess_admin_last_login', $row->last_login);
                $this->session->set_userdata('sess_admin_last_ip', $row->last_ip);
                $this->session->set_userdata('sess_admin_status', $row->status);

                redirect('admin/dashboard');
            } else {
                $this->session->set_flashdata('message', 'Error ! Invalid Login..');
                $this->template->build('pages/admin_login');
            }
        } else {
            $this->session->set_flashdata('message', 'Error ! Invalid Login..');
            $this->template->build('pages/admin_login');
        }
    }
}
?>